/*
 * @Author: your name
 * @Date: 2021-12-23 16:50:17
 * @LastEditTime : 2022-05-10 10:58:56
 * @LastEditors  : Shiming
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath     : \\tms-obc-web\\src\\app\\routes\\ticket-management\\components\\invoice-requested\\requested-invoice-modal\\requested-invoice-modal.component.ts
 */
import { ChangeDetectorRef, Component, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STChange, STColumn, STComponent, STRequestOptions } from '@delon/abc/st';
import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';

import { TicketService } from '../../../services/ticket.service';

@Component({
  selector: 'app-requested-invoice-modal',
  templateUrl: './requested-invoice-modal.component.html',
  styleUrls: ['./requested-invoice-modal.component.less']
})
export class RequestedInvoiceModalComponent {
  @ViewChild('st1', { static: false })
  st1!: STComponent;
  columns: STColumn[] = this.initST();
  id: any;
  selectedRows: any[] = [];

  constructor(
    public service: TicketService,
    private nzModalService: NzModalService,
    private modal: NzModalRef,
    private router: Router,
    private cdr: ChangeDetectorRef
  ) {}

  beforeReq = (requestOptions: STRequestOptions) => {
    Object.assign(requestOptions.body, { vatappHId: this.id });
    return requestOptions;
  };

  afterRes = (data: any[], rawData?: any) => {
    return data.map(item => ({ ...item, isDelete: data?.length > 1 }));
  };

  /**
   * 移除订单
   *
   * @returns
   */
  removeOrder(item: any[]) {
    if (this.st1.total <= 1 || item?.length === this.st1.total) {
      this.service.msgSrv.warning('开票申请记录不能少于一个订单');
      return;
    }
    if (item?.length <= 0) {
      this.service.msgSrv.warning('请选择要移除订单');
      return;
    }
    this.nzModalService.warning({
      nzTitle: '确定从当前批次中移除所选订单？',
      nzContent: '移除后相关订单可以重新提交开票申请',
      nzOnOk: () => {
        const ids = item.map(order => order.billHCode);
        this.service.request(this.service.$api_remove_bill, { billHcodes: ids }).subscribe(res => {
          if (res) {
            this.service.msgSrv.success('移除成功');
            this.modal.destroy(true);
          } else {
            this.service.msgSrv.warning('移除失败');
          }
        });
      }
    });
  }
  stChange(e: STChange): void {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox!;
        break;
      case 'filter':
        this.st1.load();
        break;
    }
  }
  private initST(): STColumn[] {
    return [
      { title: '', index: 'key', type: 'checkbox' },
      { title: '订单号', index: 'billHCode', width: 150 },
      { title: '订单完成日期', index: 'billfinTime', type: 'date', width: 150 },
      { title: '所属项目', index: 'projectIdName', width: 250 },
      { title: '订单类型', index: 'billTypeLabel', width: 90 },
      { title: '装货地', index: 'loadingfrom', width: 250 },
      { title: '卸货地', index: 'loadingto', width: 250 },
      { title: '货物信息', index: 'goodsinfo', width: 170 },
      { title: '承运司机', index: 'driverinfo', width: 280 },
      {
        title: '总费用',
        index: 'billkpmoney',
        width: 90,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }: any) => ({ value: record.billkpmoney }) }
      },
      {
        title: '运输费',
        index: 'fjfmoney2',
        width: 90,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }: any) => ({ value: record.fjfmoney2 }) }
      },
      {
        title: '附加费',
        index: 'fjfmoney',
        width: 90,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }: any) => ({ value: record.fjfmoney }) }
      },
      {
        title: '操作',
        width: 80,
        fixed: 'right',
        className: 'text-center',
        buttons: [
          {
            text: '移除',
            click: (item: any) => this.removeOrder([item]),
            iif: item => item.isDelete
          }
        ]
      }
    ];
  }
  saveManage() {
    if (this.selectedRows?.length <= 0) {
      this.service.msgSrv.warning('请选择订单');
      return;
    }
    const selectedRows = this.selectedRows.map(item => {
      return { ...item };
    });
    const params = {
      ficoVatappBillVOList: selectedRows.map(item => {
        delete item._values;
        return item;
      }),
      id: this.id
    };
    this.service.request(this.service.$api_get_applyFicoVatinv, params).subscribe((res: any) => {
      if (res) {
        this.nzModalService.confirm({
          nzTitle: '是否进入销票处理页面完成开票',
          nzOnOk: () => {
            this.service.msgSrv.success('提交成功');
            this.modal.destroy(true);
            this.router.navigate(['/ticket/cancellation-invoice']);
          },
          nzOnCancel: () => {
            this.modal.destroy(true);
          }
        });
      }
    });
  }
}
